package com.scalar.chiptrack.operations.finishedGoods.actionCommands;

import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.finishedGoods.forms.FinishedGoodsEntryForm;
import com.scalar.chiptrack.operations.finishedGoods.dao.FinishedGoodsDAO;
import com.scalar.chiptrack.operations.finishedGoods.EquivalentPartNumber;
import com.scalar.chiptrack.operations.finishedGoods.FinishedGoodsHelper;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.processtypesetup.dao.ProcessTypeSetupDAO;
import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Murali Krishna Yengala
 * Date: May 28, 2005
 * Time: 10:39:22 AM
 */
public class FinishedGoodsLoadCommand extends ChipTrackExtendedAction{

        public ActionForward executeRequest( ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response) throws Exception
        {
            HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
            HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
            FinishedGoodsEntryForm finishedGoodsEntryForm = ( FinishedGoodsEntryForm ) form;
            String loadType = request.getParameter( "loadType" );
            loadType = loadType == null ? "" : loadType.trim();

            FinishedGoodsHelper.resetFinishedGoodsEntryForm( finishedGoodsEntryForm);
            int processStepIdI = -1;
            try
            {
                String processStepIdS = finishedGoodsEntryForm.getProcessStepId();
                processStepIdS = (processStepIdS == null ? "" : processStepIdS.trim());
                processStepIdI = Integer.parseInt( processStepIdS);
            }
            catch( NumberFormatException e)
            {
                e.printStackTrace();
                errors.put( "Finished Goods Not Found", "");
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors);
                FinishedGoodsHelper.setBaseProductsAndCustomers( request, errors);
                request.setAttribute("fgiProductsList", new ArrayList());
                return mapping.findForward("finishedGoodsSearch");
            }
            try
            {
                FinishedGoodsDAO.loadProductInfoByProcessStepId( processStepIdI, finishedGoodsEntryForm );
            }
            catch( ScalarException e)
            {
                e.printStackTrace();
                errors.put( "Finished Goods Not Found", "");
                request.setAttribute( SessionKeys.ERROR_MESSAGES, errors);
                FinishedGoodsHelper.setBaseProductsAndCustomers( request, errors);
                request.setAttribute("fgiProductsList", new ArrayList());
                return mapping.findForward("finishedGoodsSearch");
            }
            finishedGoodsEntryForm.setProcessStepIdI( processStepIdI);
            String basePartIdS = finishedGoodsEntryForm.getBaseProductId();
            int basePartIdI = -1;
            basePartIdS = basePartIdS == null ? "" : basePartIdS.trim();
            try
            {
                basePartIdI = Integer.parseInt( basePartIdS );
            }
            catch( NumberFormatException e)
            {
                e.printStackTrace();
            }
            String basePartNo = ProductDao.getBaseProductName( finishedGoodsEntryForm.getBaseProductId());
            finishedGoodsEntryForm.setBaseProductNo( basePartNo);
            finishedGoodsEntryForm.setBaseProductIdI( basePartIdI );
            int customerId = -1;
            try
            {
                String customerIdStr = finishedGoodsEntryForm.getCustomerId();
                customerIdStr = customerIdStr == null ? "" : customerIdStr.trim();
                customerId = Integer.parseInt( customerIdStr);
                String customerName = CustomerDAO.loadCustomerName( customerId);
                finishedGoodsEntryForm.setCustomerName( customerName);
            }
            catch( Exception e)
            {
                e.printStackTrace();
                errors.put( "Customer for this Finished Goods, Not Found", "");
            }
            finishedGoodsEntryForm.setCustomerIdI( customerId);
            FinishedGoodsHelper.setFgiProducts( basePartIdI, request, errors, info);
            FinishedGoodsHelper.setCustomers( request, errors );
            String[] equivalentPartS = ProductDao.loadProductDupNames( processStepIdI);
            ArrayList equiArrayList = new ArrayList();
            if( equivalentPartS != null)
            {
                for( int i = 0; i < equivalentPartS.length; i++)
                {
                    EquivalentPartNumber dupPartNo = new EquivalentPartNumber();
                    dupPartNo.setPartNo( equivalentPartS[i]);
                    equiArrayList.add( dupPartNo);
                }
                if( equiArrayList.size() == 0)
                {
                    if( "view".equalsIgnoreCase( loadType))
                    {
                        equiArrayList = null;
                    }
                    else
                    {
                        equiArrayList.add( new EquivalentPartNumber());
                    }
                }
                finishedGoodsEntryForm.setEquivalentPartNosList( equiArrayList);
            }
            ArrayList processParameters = null;
            try
            {
                processParameters = FinishedGoodsDAO.loadProcessParametersByProcessStepId( processStepIdI);
            }
            catch( ScalarException e )
            {
                errors.put(ERROR_INTERNAL_SERVER_ERROR, null);
            }
            finishedGoodsEntryForm.setParametersList( processParameters );
            finishedGoodsEntryForm.setVendorList ( VendorDAO.getFinishedGoodsLocations ( processStepIdI ));
            String[] fgiProcessStepIds = FinishedGoodsDAO.loadFgiProcessStepIdsByFgProcessStepId( processStepIdI);
            finishedGoodsEntryForm.setFgiProcessStepIds( fgiProcessStepIds);
            String[] customerIds = FinishedGoodsDAO.loadCustomersForFG( processStepIdI );
            finishedGoodsEntryForm.setCustomerIds( customerIds );

            request.setAttribute(SessionKeys.ERROR_MESSAGES, errors);
            request.setAttribute(SessionKeys.INFO_MESSAGES, info);

            request.setAttribute( "units", ProcessTypeSetupDAO.getUnitsOfMesurment() );
            request.setAttribute( "algorithmList", FinishedGoodsDAO.loadAlgorithmList());
            if( "view".equalsIgnoreCase( loadType))
            {
                return  mapping.findForward("view");
            }
            else
            {
                saveToken( request);
                return  mapping.findForward("editFg");
            }
        }
}